//---------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------- SPACE ------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------
	function Space(space, piso){
		
		//inicializacion
		var _this = this;
		this.id = space.id; //06/02/2012
		
		//if (this.id == "45" && piso.name =="CA_PB") console.log('45--> ',this);
		
		this.getPath = function(){
			return {
				edificio: this.piso.buildingName,
				nivel: this.piso.level,
				piso: this.piso.name, 
				space: this.name
			}
		}
		
		this.name = space.name;
		this.type = space.type;
		this.contour = {};
		
		this.height = space.height;
		this.relativeHeightToFloor = space.height - piso.absoluteHeight;
		
		this.contour.centroid = {
						x: space.contour.centroid.x,
						y: this.relativeHeightToFloor,
						z: space.contour.centroid.y,
				 };
		
		this.contour.points = [];
		this.paredes = [];
		
		this.piso = piso; //puntero al nodo padre (piso en el que esta ubicado)
		this.outgoingPortals = [];
		
		this.contourScaled = [];
		
//		this.isMezzanine = function(){
//			for (var i = 0, l = this.outgoingPortals.length; i < l; i++){
//				if (this.outgoingPortals[i].clase == "inter_floor"){
//					if (this.outgoingPortals[i].sourceSpace.piso.level != this.outgoingPortals[i].targetSpace.piso.level){
//						return true;
//					}
//				} 
//			}
//			return false;
//		}
		
		//contorno con representacion de puntos del servidor
		function generarParedes(contour){			
			//adapto los puntos a la representacion del espacio 3D (vs. posiciones provenientes del editor)
			//var contour = space.contourScaled;
//			var contour = space.contour.points;
			if (!contour.length) return;
			
			var pared = [];
			for (var i = 0, l = contour.length; i <= l; i++){
				indice = i % l;
				
				pared.push({
					x: contour[indice].x,
					y: _this.relativeHeightToFloor,
					z: contour[indice].y,
				});
				
				if(contour[indice].isPortal){
					if (contour[indice].isUStair){
						pared.pop(); 
						pared.pop(); 
						i += 2;
						//console.log('es UUUUUUUUUUUUUUUUUUU')
					}
					
					if(pared.length > 1){
						if (pared.length == 2){
							var p1 = pared[0];
							var p2 = pared[1];
							var ancho = Math.sqrt((p1.x-p2.x)^2 + (p1.y-p2.y)^2 );
							if (ancho > 20){
								_this.paredes.push(pared);
							}
						}else{
							_this.paredes.push(pared);
						}
						
					}
					pared = [];
				}//fin portal
			}
			
			
			if(pared.length > 1){
				_this.paredes.push(pared);
			}
		}
		
		function borrarColineales(l){
			for (var i = 0, s = l.length; i < s; i++){
				if( isColineal( l[i] , l[(i+1)%s] , l[(i+2)%s] )){
					l.splice( (i+1)%s ,1 );
					i--;//repito con el mismo i porque va 1 mas lejos
					s--;//uno menos de limite
				}
			}
		}
		
		function isColineal(p1, p2, p3){
			return (p1.x == p2.x && p2.x == p3.x || p1.y == p2.y && p2.y == p3.y);
		}
		
		
		function mapearRepServidor(contour){
			var _thisContour = []; //_this.contour.points;
			for (var i = 0, l = contour.length; i < l; i++){
				_thisContour.push({
					x: contour[i].x,
					y: _this.relativeHeightToFloor,
					z: contour[i].y,
				});
			}
			return _thisContour;
		}
		
		
		//representacion de puntos del servidor
		function scale(contour){
			
			var cent = _this.contour.centroid;
			var matrixT1 = new THREE.Matrix4();
			var matrixT2 = new THREE.Matrix4();
			var matrixT3 = new THREE.Matrix4();
			matrixT1.makeTranslation(-cent.x, -cent.z,0);//llevo al origen
			matrixT2.makeScale(1.05,1.05,0) //escalo
//			matrixT2.makeScale(0.5,0,0.5) //escalo
			matrixT3.makeTranslation(cent.x, cent.z, 0);//dejo el punto donde estaba
			
//			matrixT1.multiplySelf(matrixT2.multiplySelf(matrixT3))
			var contourToReturn = [];
			
			for (var i = 0, l = contour.length; i < l; i++){
				var c = contour[i];
				var point = new THREE.Vector3(c.x, c.y, 0);
				point = matrixT3.multiplyVector3(matrixT2.multiplyVector3(matrixT1.multiplyVector3(point)));
				if (contour[i].isPortal){point.isPortal = true;}
				contourToReturn.push(point);
			}
			//console.log('p',_this.contour.points,'scaled p', _this.scaledContour);
			return contourToReturn;
		};
		
		
		function scaleAMano(contour){
			
			var contourToReturn = [];
			var p1, p2;
			var valor = 2;
			
			//copy
			for (var i = 0, l = contour.length; i < l; i++){
				contourToReturn[i] = {x: contour[i].x, y: contour[i].y}
				if (contour[i].isPortal) contourToReturn[i].isPortal = true;
				if (contour[i].isStair) contourToReturn[i].isStair = true;
				if (contour[i].isUStair) contourToReturn[i].isUStair = true;
			}
			
			for (var i = 0, l = contourToReturn.length; i < l; i++){
				p1 = contourToReturn[i%l];
				p2 = contourToReturn[(i+1)%l];
				
				if (p1.x == p2.x){
					if (p1.y > p2.y){
						p1.x -= valor;
						p2.x -= valor;
					}else{
						p1.x += valor;
						p2.x += valor;
					}
				}
				
				if (p1.y == p2.y){
					if (p1.x < p2.x){
						p1.y -= valor;
						p2.y -= valor;
					}else{
						p1.y += valor;
						p2.y += valor;
					}
				}
				
			}
		
			return contourToReturn;
		}
		
		
		
		//console.log('space___ ->',_this.id, 'floor', _this.piso.id)
		var contourServerScaled = scaleAMano(space.contour.points);
		generarParedes(contourServerScaled);
		_this.contourScaled = mapearRepServidor(contourServerScaled);
		
		borrarColineales(space.contour.points);
		_this.contour.points = mapearRepServidor(space.contour.points);
		
	};
	
	
	
	